package com.huawei.petSystemdemo.ui.Race;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huawei.petSystemdemo.MainActivity2;
import com.huawei.petSystemdemo.R;
import com.huawei.petSystemdemo.adapter.PetAdapter;
import com.huawei.petSystemdemo.bean.Pets;
import com.huawei.petSystemdemo.utils.ImgRepository;
import com.huawei.petSystemdemo.utils.MyRequest;
import com.huawei.petSystemdemo.utils.OnBindBottomListener;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.loader.ImageLoader;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.ArrayList;
import java.util.BitSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class CatActivity extends AppCompatActivity {
    private Banner mBanner;
    private int current = 1;
    private int size = 8;
    private String race;
    private ImageView race1;
    private ImageView race2;
    private ImageView race3;
    private ImageView race4;
    private TextView race_text1;
    private TextView race_text2;
    private TextView race_text3;
    private TextView race_text4;


    Map<String, Object> requestBody = new HashMap<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cat);

        Intent intent = getIntent();
        race = intent.getStringExtra("race");
        System.out.println();

//轮播图
        mBanner = findViewById(R.id.catBanner);
        //图片资源
        ArrayList<Integer> imgeList = new ArrayList<>();
        imgeList.add(R.drawable.one);//one
        imgeList.add(R.drawable.two);//two
        imgeList.add(R.drawable.three);//three
        imgeList.add(R.drawable.four);//four


        //轮播标题
        String[] mtitle = {"图片1", "图片2", "图片3", "图片4"};
        List<String> titleList = new ArrayList<>();
        for (int i = 0; i < mtitle.length; i++) {
            //imgeList.add(imgeList[i]);//把图片资源循环放入list里面
            titleList.add(mtitle[i]);//把标题循环设置进列表里面
        }

        //设置轮播的动画效果,里面有很多种特效,可以到GitHub上查看文档。
        mBanner.setImages(imgeList);//设置图片资源
        mBanner.setImageLoader(new CatActivity.ImageLoadBanner());
        mBanner.setBannerTitles(titleList); //设置标题集合（当banner样式有显示title时）
        mBanner.setDelayTime(1000);//设置轮播时间3秒切换下一图
        mBanner.isAutoPlay(true);
        mBanner.setIndicatorGravity(BannerConfig.CENTER);
        mBanner.setBannerAnimation(Transformer.Accordion);
        mBanner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE_INSIDE);//设置banner显示样式（带标题的样式）
        mBanner.start();//开始进行banner渲染

        //图片文字处理
        race1 = findViewById(R.id.race1);
        race2 = findViewById(R.id.race2);
        race3 = findViewById(R.id.race3);
        race4 = findViewById(R.id.race4);
        race_text1 = findViewById(R.id.race_text_1);
        race_text2 = findViewById(R.id.race_text_2);
        race_text3 = findViewById(R.id.race_text_3);
        race_text4 = findViewById(R.id.race_text_4);
        if(race.equals("猫")) {
            race1.setImageResource(R.drawable.jiafei);
            race2.setImageResource(R.drawable.buou);
            race3.setImageResource(R.drawable.jinjianceng);
            race4.setImageResource(R.drawable.yinjianceng);
            race_text1.setText("加菲猫");
            race_text2.setText("布偶猫");
            race_text3.setText("金渐层");
            race_text4.setText("银渐层");
        } else if(race.equals("狗")) {
            race1.setImageResource(R.drawable.taidi);
            race2.setImageResource(R.drawable.jinmao);
            race3.setImageResource(R.drawable.caiquan);
            race4.setImageResource(R.drawable.keji);
            race_text1.setText("泰迪");
            race_text2.setText("金毛");
            race_text3.setText("柴犬");
            race_text4.setText("柯基");
        } else if(race.equals("小宠物")){
            race1.setImageResource(R.drawable.mouseone);
            race2.setImageResource(R.drawable.mousetwo);
            race3.setImageResource(R.drawable.mousethree);
            race4.setImageResource(R.drawable.mousefour);
            race_text1.setText("仓鼠");
            race_text2.setText("龙猫");
            race_text3.setText("荷兰猪");
            race_text4.setText("花枝鼠");

        }else {

        }



        RecyclerView list = findViewById(R.id.shop_list_pet);

        LinearLayoutManager manager = new LinearLayoutManager(this);
        list.setLayoutManager(manager);

        List<Pets> petsList = new ArrayList<>();

        requestBody.put("race", race + "");
        requestBody.put("current", current + "");
        requestBody.put("size", size + "");


        MyRequest.get("/pet/getRace",  requestBody, new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                System.out.println("当前无数据");
                e.printStackTrace();
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String json = response.body().string();
                System.out.println(json);
                JSONObject obj = JSON.parseObject(json);
                System.out.println(json);
                //int statusCode = obj.getInteger("statusCode");
                String errMessage = obj.getString("errMessage");
                List<Pets> data = obj.getJSONArray("data").toJavaList(Pets.class);
                //System.out.println(statusCode);
                System.out.println(errMessage);
                System.out.println(data);

                petsList.addAll(data);
            }
        });


        PetAdapter petAdapter = new PetAdapter(petsList, getBaseContext());
        petAdapter.setListener(new OnBindBottomListener<List<Pets>>() {
///
            @Override
            public void bind(List<Pets> list1) {
                current++;
                requestBody.put("race", race + "");
                requestBody.put("current", current + "");
                requestBody.put("size", size + "");
                MyRequest.get("/pet/getRace", requestBody, json -> {
                    System.out.println(json);
                    JSONObject obj = JSON.parseObject(json);
                    int statusCode = obj.getInteger("statusCode");
                    String errMessage = obj.getString("errMessage");
                    List<Pets> data = obj.getJSONArray("data").toJavaList(Pets.class);
                    System.out.println(statusCode);
                    System.out.println(errMessage);
                    System.out.println(data);

                    if (data.size() != 0)
                    {
                        System.out.println("新数据");
                        petsList.addAll(data);
                        petAdapter.notifyDataSetChanged();
                    }
                });
            }
        });
        list.setAdapter(petAdapter);
    }


    class ImageLoadBanner extends ImageLoader {
        public void displayImage(Context context, Object path, ImageView imageView) {
            imageView.setImageResource(Integer.parseInt(path.toString()));
        }
    }
    public void onClick (android.view.View back){
        Intent intent = new Intent(this, MainActivity2.class);//界面跳转
        startActivity(intent);//

    }

}
